﻿package Cain.control 
{
	/**
	 * Bar資料核心
	 * @author Cain
	 */
	public class BarModel
	{
		/**
		 * 數值變動
		 */
		public static const CHING:String = "ching";
		private var n:Number;
		private var max:Number;
		private var min:Number;
		private var num_off:Boolean;
		private var d:Number;
		private var hy:Number;
		private var dy:Number;
		private var dd:Number;
		private var now_y:Number;
		/**
		 * 資料建構
		 * @param	_max 最高數值
		 * @param	_min 最低數值
		 * @param	_Num 是否使用小數
		 */
		public function BarModel(_max:Number,_min:Number,_Num:Boolean = false) 
		{
			max = _max;
			min = _min;
			num_off = _Num;
			d = max - min;
			num = min;
		}
		/**
		 * Bar位置建構
		 * @param	_hy 最高點y
		 * @param	_dy 最低點y
		 * @param	_num 預設值
		 * @return 根據預設值設定的y
		 */
		internal function setup(_hy:Number,_dy:Number,_num:Number):Number 
		{
			hy = _hy;
			dy = _dy;
			num = _num;
			dd = Math.abs(hy - dy) / d;
			now_y = dy - dd * (num-min);
			return now_y;
			
		}
		/**
		 * 設定目前數值
		 */
		internal function set num(_n:Number):void 
		{
			n = _n;
			if (!num_off) 
			{
				n = Math.floor(n);
			}
			now_y = dy - dd * (num-min);
		}
		internal function get num():Number 
		{
			return n;
		}
		/**
		 * bar的y的對應值
		 */
		internal function set now(_yy:Number):void 
		{
			now_y = _yy;
			if (now_y<hy) 
			{
				now_y = hy;
			}else if(now>dy) 
			{
				now_y = dy;
			}
			var a:Number = Math.abs(now_y - dy);
			var b:Number = a / dd;
			if (!num_off)
			{
				b = Math.floor(b);
			}
			n = b+min;
		}
		internal function get now():Number 
		{
			return now_y;
		}
	}

}